﻿namespace WPForms.Phone.DataAccess
{
    using System;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    /// <summary>
    /// Partial class for the attachment db model class.    
    /// </summary>
    public partial class Attachment
    {
        /// <summary>
        /// Reference to the form.
        /// </summary>
        private EntityRef<Form> formRef = new EntityRef<Form>();

        /// <summary>
        /// Gets or sets the form.
        /// </summary>
        [Association(Name = "FK_Form_Attachments", Storage = "formRef", ThisKey = "FormId", OtherKey = "Id", IsForeignKey = true)]
        public Form Form
        {
            get
            {
                return this.formRef.Entity;
            }

            set
            {
                var previousValue = this.formRef.Entity;
                if (previousValue != value || this.formRef.HasLoadedOrAssignedValue == false)
                {
                    if (previousValue != null)
                    {
                        this.formRef.Entity = null;
                        previousValue.Attachments.Remove(this);
                    }

                    this.formRef.Entity = value;
                    if (value != null)
                    {
                        value.Attachments.Add(this);
                        this.FormId = value.Id;
                    }
                    else
                    {
                        this.FormId = 0;
                    }
                }
            }
        }
    }
}
